﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;

namespace pyTivoMetadataManager
{
    public partial class LoadForm : Form
    {
        private MainForm _mainForm;

        public LoadForm(MainForm mainForm)
        {
            InitializeComponent();
            _mainForm = mainForm;
        }

        private void LoadForm_Load(object sender, EventArgs e)
        {
            if (!_mainForm.CurrentMediaFile.Extension.ToLower().Equals(".tivo"))
            {
                tcTabs.TabPages.Remove(tpTivo);
            }
            tcTabs.TabPages.Remove(tpTheTvDb);
            tcTabs.TabPages.Remove(tpTheMovieDB);

            txtNetflixTitle.Text = txtImdbName.Text = _mainForm.MediaName;            
        }

        private static string _imdbSearchFormat = "http://www.imdb.com/find?s=tt&q={0}";
        private void btImdbSearch_Click(object sender, EventArgs e)
        {
            string searchUrl = string.Format(_imdbSearchFormat, Uri.EscapeDataString(txtImdbName.Text.Trim()));
            try
            {
                Process.Start(searchUrl);
            }
            catch (Win32Exception noBrowser)
            {
                if (noBrowser.ErrorCode == -2147467259)
                {
                    MessageBox.Show(noBrowser.Message);
                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnTivoImport_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Not implemented yet");
        }

        private string _titleFormat = "{0} ({1})";
        private NetflixDataManager dmNetflix = new NetflixDataManager();
        private void btnNetflixFind_Click(object sender, EventArgs e)
        {
            NetflixAPI napi = new NetflixAPI();
            dmNetflix.Titles = napi.SearchTitles(txtNetflixTitle.Text.Trim());
            lvNetflixTitles.Items.Clear();
            lvNetflixTitles.Enabled = true;
            foreach (string key in dmNetflix.Titles.Keys)
            {
                NetflixTitleInfo nti = dmNetflix.Titles[key];
                ListViewItem lvi = new ListViewItem();
                lvi.Tag = nti;
                lvi.Text = string.Format(_titleFormat, nti.Title, nti.ReleaseYear);
                lvNetflixTitles.Items.Add(lvi);
            }
        }

        private void lvNetflixTitles_DoubleClick(object sender, EventArgs e)
        {
            NetflixApply();
            this.Close();
        }

        private void NetflixApply()
        {
            NetflixTitleInfo nti = (NetflixTitleInfo)lvNetflixTitles.SelectedItems[0].Tag;
            if (nti != null)
            {
                new NetflixAPI().GetTitleDetails(ref nti);

                VideoMetadata vm = _mainForm.Metadata;
                if (!string.IsNullOrEmpty(nti.Title)) vm.Title = nti.Title;
                if (!string.IsNullOrEmpty(nti.Synopsis)) vm.Description = nti.Synopsis;
                if (nti.Genres.Count > 0)
                {
                    vm.Genres.Clear();
                    vm.Genres.AddRange(nti.Genres);
                }
                vm.MovieYear = nti.ReleaseYear;
                vm.OriginalAirDate = new DateTime(nti.ReleaseYear, 1, 1);                

                if (nti.Directors.Count > 0 || nti.Cast.Count > 0)
                {
                    vm.People.Clear();
                }
                if (nti.Directors.Count > 0)
                {
                    foreach (string director in nti.Directors)
                    {
                        vm.People.Add(new Person() { Name = director, Type = PersonType.Director });
                    }
                }
                if (nti.Cast.Count > 0)
                {
                    foreach (string actor in nti.Cast)
                    {
                        vm.People.Add(new Person() { Name = actor, Type = PersonType.Actor });
                    }
                }
                vm.StarRating = "x" + (((int)(nti.Rating * 2)) - 1).ToString();
                if (!string.IsNullOrEmpty(nti.TvRating)) vm.TvRating = nti.TvRating;
                if (!string.IsNullOrEmpty(nti.MpaaRating)) vm.MpaaRating = nti.MpaaRating;

                _mainForm.LoadMetadata(vm);
            }
        }

        private void lvNetflixTitles_SelectedIndexChanged(object sender, EventArgs e)
        {
            btnImport.Enabled = true;
        }

        private void btnImport_Click(object sender, EventArgs e)
        {
            if (tcTabs.SelectedTab.Equals(tpNetflix))
            {
                NetflixApply();
                this.Close();
            }
        }
    }
}
